import api from "@/api"
import {transformRoutesByMenus} from '@/utils/transform'
export default{
    namespaced:true,
    state:{
        isCollapse:false,
        menus:JSON.parse(localStorage.getItem('menus'))||[],
        homeChildRoutes:JSON.parse(localStorage.getItem('routes'))||[]
    },
    mutations:{
        SET_COLLAPSE(state,payload){
            state.isCollapse=payload
        },
        SET_MENUS(state,payload){
            state.menus=payload
            localStorage.setItem("menus",JSON.stringify(payload))
        },
        SET_ROUES(state,payload){
            state.homeChildRoutes=payload
            localStorage.setItem("routes",JSON.stringify(payload))
        },
        REMVOE_MENUS(){
            state.menus=[]
            localStorage.removeItem("menus")
        },
        REMOVE_ROUTES(){
            state.homeChildRoutes=[]
            localStorage.removeItem("routes")
        }
    },
    actions:{
        async getMenusAsync({commit}){
            const result=await api.users.getAuthMenusApi()
            commit('SET_MENUS',result.data)
        },
        async getHomeChildRoutesAsync({commit}){
            const result=await api.users.getAuthMenusApi()
            let routes=[]
            transformRoutesByMenus(result.data,routes)
            commit('SET_ROUES',routes)
        }
    }
}